package exception;

import java.util.Scanner;

/**
 * 异常练习
 */
public class ExceptionExercise01 {

    public static void main(String[] args) {
        /*
            1)编写应用程序 EcmDef 接收命令行两个参数，计算两数相除
            2)计算两个数相除，要求使用方法 cal(int n1, int n2)
            3)对数据格式不正确(NumberFormatException)
              缺少命令行参数(ArrayIndexOutOfBoundsException)
              除0(ArithmeticException)
              进行异常处理
         */
        Scanner scanner = new Scanner(System.in);
        int res;
        while(true) {
            try {
                System.out.println("请输入被除数：");
                String n1 = scanner.next();
                System.out.println("请输入除数：");
                String n2 = scanner.next();

                int i1 = Integer.parseInt(n1);
                int i2 = Integer.parseInt(n2);

                res = EcmDef.cal(i1, i2);
                System.out.println("结果=" + res);
                break;
            } catch (NumberFormatException e) {
                System.out.println("数据格式不正确，请重新输入");
            } catch (ArithmeticException e) {
                System.out.println("除数不能为0，请重新输入");
            } catch (ArrayIndexOutOfBoundsException e) {
                System.out.println("命令行参数不能为空，请重新输入");
            }

        }

    }
}

class EcmDef {

    public static int cal(int n1, int n2) {
        return n1 / n2;
    }
}